/***************************************************************************
 *   oboe.controllers.SceneController                                      *
 *   Copyright (C) 2011 by Felipe Manga                                    *
 *   lufeboma@gmail.com                                                    *
 *                                                                         *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the       *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to    *
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:                                             *
 *                                                                         *
 *   The above copyright notice and this permission notice shall be        *
 *   included in all copies or substantial portions of the Software.       *
 *                                                                         *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
 ***************************************************************************/

package oboe.controllers
{
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import oboe.core.Oboe;
	import oboe.core.SigSlot;
	
	public class ScrollbarController extends SigSlot
	{
		private var rect:Rectangle;
		private var container:DisplayObjectContainer;
		private var track:DisplayObjectContainer;
		private var asset:DisplayObject;
		private var relOffset:Number;
		private var fullHeight:Number;
		
		private static var hackBMP:BitmapData = new BitmapData(1,1);
		
		public function ScrollbarController(parent:DisplayObjectContainer, displayObject:DisplayObject=null)
		{
			this.asset = displayObject;
			if( !this.asset )
			{
				var s:Sprite = new Sprite();
				s.graphics.beginFill( 0xFFFFFF, 0.9 );
				s.graphics.drawRect( parent.scrollRect.width-10, 0, 10, 10 );
				s.graphics.endFill();
				this.asset = s;
			}
			
			super();
			if( !this.asset.parent ) parent.addChild( this.asset );
			this.asset.addEventListener( MouseEvent.MOUSE_DOWN, onMouseDown );
			this.asset.addEventListener(Event.REMOVED_FROM_STAGE, onStopDrag );
			
			this.track = this.asset.parent;
			this.track.parent.addChild( this.asset );
			this.track.addEventListener( MouseEvent.MOUSE_DOWN, onMouseDownTrack );

			this.container = parent;
			this.rect = this.container.scrollRect;
			if( !this.rect ) this.rect = this.container.getRect(this.container);
			resizeBar();
			this.asset.y = this.track.y;
			this.asset.x += this.track.x;
		}
		
		[slot] public function onParentConstructed():void
		{
			this.resizeBar();
		}
		
		private function resizeBar():void
		{
			this.container.scrollRect = null;
			hackBMP.draw( this.container );			
			
			this.fullHeight = this.container.height / this.container.scaleY;
			var ratio:Number = rect.height / fullHeight;
			
			if( ratio >= 1 )
			{
				this.asset.visible = false;
				this.track.visible = false;
				return;
			}
						
			if( "middle" in this.asset )
			{
				this.asset.scaleY = 1;
				var r:Rectangle = this.asset.getBounds( this.asset.parent );
				with( asset as Object )
				{
					top.y = 0;
					middle.y = top.height;
					middle.scaleY = 1;
					middle.height = track.height*ratio - (top.height+bottom.height);
					bottom.y = Math.floor(middle.y + middle.height);
				}
			}
			else
			{
				this.asset.scaleY = track.height*ratio/(this.asset.height/this.asset.scaleY);
			}
			
			this.container.scrollRect = rect;
		}
		
		private function onMouseDownTrack( e:MouseEvent ):void
		{
			this.resizeBar();
			
			this.asset.y = this.track.y + e.localY - this.asset.height/2;
			this.relOffset = Oboe.stage.mouseY;
			var pos:Number = (this.asset.y-this.track.y) / (this.track.height-this.asset.height);
			if( pos < 0 )
			{
				this.asset.y = this.track.y;
				pos = 0;
			}
			if( pos > 1 )
			{
				this.asset.y = this.track.y + this.track.height - this.asset.height;
				pos = 1;
			}
			this.rect.y = (this.fullHeight - this.rect.height) * pos;
			this.container.scrollRect = null;
			this.container.scrollRect = this.rect;
			this.message("onUpdate");
		}
		
		private function onMouseDown( e:MouseEvent ):void
		{
			relOffset = Oboe.stage.mouseY;
			Oboe.stage.addEventListener( MouseEvent.MOUSE_MOVE, onMouseMove );
			Oboe.stage.addEventListener( MouseEvent.MOUSE_UP, onStopDrag    );
		}
		
		private function onMouseMove( e:MouseEvent ):void
		{
			if( e.buttonDown == false )
			{
				this.onStopDrag();
				return;
			}
			
			this.resizeBar();
			
			var delta:Number = Oboe.stage.mouseY - this.relOffset;
			this.asset.y += delta;
			this.relOffset = Oboe.stage.mouseY;
			var pos:Number = (this.asset.y-this.track.y) / (this.track.height-this.asset.height);
			if( pos < 0 )
			{
				this.asset.y = this.track.y;
				pos = 0;
			}
			if( pos > 1 )
			{
				this.asset.y = this.track.y + this.track.height - this.asset.height;
				pos = 1;
			}
			this.rect.y = (this.fullHeight - this.rect.height) * pos;
			this.container.scrollRect = null;
			this.container.scrollRect = this.rect;
			this.message("onUpdate");
		}
		
		private function onStopDrag(...x):void
		{
			Oboe.stage.removeEventListener( MouseEvent.MOUSE_MOVE, onMouseMove );
			Oboe.stage.removeEventListener( MouseEvent.MOUSE_UP, onStopDrag );
		}
	}
}